package action.teacher;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import util.ExportScore;

import com.opensymphony.xwork2.ActionSupport;
import model.*;

public class ScoreAction extends ActionSupport 
{
	private String actiontype="1";//actiontype用于标识是获取所有的记录还是搜索记录,默认是获取所有记录即”1“
	
	private String actionname = "getallscores";//action名称
	
	private String stunumber;//学生学号
	
	private String stuname;//学生姓名
	
	private String stusex;//学生性别
	
	private String stuspecialId;//学生专业id
	
	private String specialdepartmentId;//学生学院id
	
	private String coursename;//课程名称
	
	private String coursetypeId;//课程类别id
	
	private int currentpage=1;//当前页数,当获得的值为null或""时初始化为1;
	
	private int pageresult=20;//每页显示数据的条数,当获得的值为null或""时初始化为20;
	
	private int pagecount;//总页数
	
	private int resultcount;//总记录
	
	
	
	//添加页面的属性
	private float kaoshifen;//考试分数
	
	private float pingshifen;//平时分数
	
	private String studentcourseId;//学生选课id
	
	//修改页面的属性
	private String id;//分数表id
	
	private List<Object> objects = new ArrayList<Object>();
	private List<Score> scores = new ArrayList<Score>();
	private List<Studentcourse> studentcourses = new ArrayList<Studentcourse>();
	private List<Student> students = new ArrayList<Student>();
	private List<Course> courses = new ArrayList<Course>();
	private List<Teacher> teachers = new ArrayList<Teacher>();
	private List<Special> specials = new ArrayList<Special>();
	private List<Department> departments = new ArrayList<Department>();
	private List<Coursetype> coursetypes = new ArrayList<Coursetype>();
	
	private List<Coursetype> totalcoursetypes = new ArrayList<Coursetype>();//存放所有的coursetype信息，用于搜索
	    
	private List<Special> totalspecials = new ArrayList<Special>();//存放某一学院对应的所有的special信息
	    
	private List<Department> totaldepartments = new ArrayList<Department>();//存放所有的department信息
	
	private ServletActionContext context;
	
	 /**
     * 验证数据合法性
     */
    public void validate()
    {
    	//this.addFieldError("msg.hello","必须输入hello!");
    	//this.addActionError("处理动作失败!");
    	//this.addActionMessage("提交成功");
    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        DepartmentDAO departmentdao = (DepartmentDAO)context.getBean("DepartmentDAOProxy");
        List temp_totaldepartments = departmentdao.findAll();
        for(int i=0;i<temp_totaldepartments.size();i++)
        {
        	Department d = new Department();
        	d = (Department)temp_totaldepartments.get(i);
        	totaldepartments.add(d);
        	d = null;
        }
    	this.context.getRequest().setAttribute("totaldepartments", totaldepartments);
    	
    	
        CoursetypeDAO coursetypedao = (CoursetypeDAO)context.getBean("CoursetypeDAOProxy");
        List temp_totalcoursetypes = coursetypedao.findAll();
        for(int i=0;i<temp_totalcoursetypes.size();i++)
        {
        	Coursetype ct = new Coursetype();
        	ct = (Coursetype)temp_totalcoursetypes.get(i);
        	totalcoursetypes.add(ct);
        	ct = null;
        }
    	this.context.getRequest().setAttribute("totalcoursetypes", totalcoursetypes);
    }
    
    /**
     * 导出学生成绩信息
     */
    public String exportScore()
    {
    	String targetDirectory = this.context.getServletContext().getRealPath("/download");
    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        
        String rightsql="te.id="+this.context.getRequest().getSession().getAttribute("teacherId");
        List temp_scores = scoredao.getAllScoreInfo(rightsql);
        System.out.println("老师总记录数----------------------------:"+temp_scores.size());
    	String resultstring = ExportScore.exportScore(temp_scores,targetDirectory);
    	this.addActionMessage("<a href='download/"+resultstring+"'>下载</a>");
    	return SUCCESS;
    }
    
    /**
     * 根据成绩表id获得记录，供修改成绩信息使用
     */
    public String getScorebyId()
    {
    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        List temp_scores = scoredao.getScorebyId(id);
        for(int i=0;i<temp_scores.size();i++)
        {
        	Object[] obj=(Object[])temp_scores.get(i);
        	
        	int begin=(currentpage-1)*pageresult;
        	int end=currentpage*pageresult;
        	if((begin<=i)&&(i<end))
        	{
        		objects.add(obj);
        		Score sc = (Score)obj[0];
            	Studentcourse stco = (Studentcourse)obj[1];
            	Student st = (Student)obj[2];
            	Course co = (Course)obj[3];
            	Teacher te = (Teacher)obj[4];
            	Special sp = (Special)obj[5];
            	Department de = (Department)obj[6];
            	Coursetype coty = (Coursetype)obj[7];
            	scores.add(sc);
            	studentcourses.add(stco);
            	students.add(st);
            	courses.add(co);
            	teachers.add(te);
            	specials.add(sp);
            	departments.add(de);
            	coursetypes.add(coty);
        	}
        	obj=null;
        }
    	return SUCCESS;
    }
    
    /**
     * 根据学生选课id获得该记录的学生、课程、教师信息,供添加成绩使用
     */
    public String getInfoByStudentcourseId()        
    {
    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentcourseDAO studentcoursedao=(StudentcourseDAO)context.getBean("StudentcourseDAOProxy");
        List temp_Studentcourse = studentcoursedao.getStudentcourseInfoByStudentcourseId(studentcourseId);
        for(int i=0;i<temp_Studentcourse.size();i++)
        {
        	Object[] obj=(Object[])temp_Studentcourse.get(i);
        		objects.add(obj);
            	Studentcourse stco = (Studentcourse)obj[0];
            	Student st = (Student)obj[1];
            	Course co = (Course)obj[2];
            	Teacher te = (Teacher)obj[3];
            	Special sp = (Special)obj[4];
            	Department de = (Department)obj[5];
            	Coursetype coty = (Coursetype)obj[6];
            	studentcourses.add(stco);
            	students.add(st);
            	courses.add(co);
            	teachers.add(te);
            	specials.add(sp);
            	departments.add(de);
            	coursetypes.add(coty);
        	
        	obj=null;
        }
    	return SUCCESS;
    }
    
	/**
	 * add score
	 * @return
	 */
	public String addScore()
	{
		System.out.println("开始添加成绩信息");
		System.out.println(kaoshifen);
		System.out.println(pingshifen);
		Score score = new Score();
		score.setStudentcourseId(Integer.parseInt(studentcourseId));
		score.setKaoshifen(kaoshifen);
		score.setPingshifen(pingshifen);
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        scoredao.save(score);
		return SUCCESS;
	}
	
	/**
	 * edit score
	 * @return
	 */
	public String editScore()
	{
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        scoredao.edit(id, pingshifen, kaoshifen);
		return SUCCESS;
	}
	
	/**
	 * delete score
	 * @return
	 */
	public String deleteScore()
	{
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        scoredao.deleteScoreById(id);
		return SUCCESS;
	}
	
	/**
	 * get all score info
	 * @return
	 */
	public String getAllScores()
	{
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        String rightsql="te.id="+this.context.getRequest().getSession().getAttribute("teacherId");
        List temp_scores = scoredao.getAllScoreInfo(rightsql);
        for(int i=0;i<temp_scores.size();i++)
        {
        	Object[] obj=(Object[])temp_scores.get(i);
        	
        	int begin=(currentpage-1)*pageresult;
        	int end=currentpage*pageresult;
        	if((begin<=i)&&(i<end))
        	{
        		objects.add(obj);
        		Score sc = (Score)obj[0];
            	Studentcourse stco = (Studentcourse)obj[1];
            	Student st = (Student)obj[2];
            	Course co = (Course)obj[3];
            	Teacher te = (Teacher)obj[4];
            	Special sp = (Special)obj[5];
            	Department de = (Department)obj[6];
            	Coursetype coty = (Coursetype)obj[7];
            	scores.add(sc);
            	studentcourses.add(stco);
            	students.add(st);
            	courses.add(co);
            	teachers.add(te);
            	specials.add(sp);
            	departments.add(de);
            	coursetypes.add(coty);
        	}
        	obj=null;
        }
        //设置总页数
        if(temp_scores.size()%pageresult==0)
        	setPagecount(temp_scores.size()/pageresult);
        else
        	setPagecount(temp_scores.size()/pageresult+1);
      //设置总记录数
        setResultcount(temp_scores.size());
		return SUCCESS;
	}
	
	/**
	 * search score info
	 * @return
	 */
	public String searchScores()
	{
		setActiontype("2");//设置action类型为搜索
    	setActionname("searchscores");//设置action名称为搜索
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ScoreDAO scoredao=(ScoreDAO)context.getBean("ScoreDAOProxy");
        String rightsql="te.id="+this.context.getRequest().getSession().getAttribute("teacherId");
        List temp_scores = scoredao.searchScoreInfo(stunumber, stuname, stusex,stuspecialId,specialdepartmentId, coursename, coursetypeId,rightsql);
        for(int i=0;i<temp_scores.size();i++)
        {
        	Object[] obj=(Object[])temp_scores.get(i);
        	int begin=(currentpage-1)*pageresult;
        	int end=currentpage*pageresult;
        	if((begin<=i)&&(i<end))
        	{
        		objects.add(obj);
        		Score sc = (Score)obj[0];
            	Studentcourse stco = (Studentcourse)obj[1];
            	Student st = (Student)obj[2];
            	Course co = (Course)obj[3];
            	Teacher te = (Teacher)obj[4];
            	Special sp = (Special)obj[5];
            	Department de = (Department)obj[6];
            	Coursetype coty = (Coursetype)obj[7];
            	scores.add(sc);
            	studentcourses.add(stco);
            	students.add(st);
            	courses.add(co);
            	teachers.add(te);
            	specials.add(sp);
            	departments.add(de);
            	coursetypes.add(coty);
        	}
        	obj=null;
        }
      //设置总页数
        if(temp_scores.size()%pageresult==0)
        	setPagecount(temp_scores.size()/pageresult);
        else
        	setPagecount(temp_scores.size()/pageresult+1);
      //设置总记录数
        setResultcount(temp_scores.size());
        return SUCCESS;
	}
	
	public String getStunumber() {
		return stunumber;
	}

	public void setStunumber(String stunumber) {
		this.stunumber = stunumber;
	}

	public String getStuname() {
		return stuname;
	}

	public void setStuname(String stuname) {
		this.stuname = stuname;
	}

	public String getCoursename() {
		return coursename;
	}

	public void setCoursename(String coursename) {
		this.coursename = coursename;
	}

	public String getCoursetypeId() {
		return coursetypeId;
	}

	public void setCoursetypeId(String coursetypeId) {
		this.coursetypeId = coursetypeId;
	}

	public float getPingshifen() {
		return pingshifen;
	}

	public void setPingshifen(float pingshifen) {
		this.pingshifen = pingshifen;
	}

	public float getKaoshifen() {
		return kaoshifen;
	}

	public void setKaoshifen(float kaoshifen) {
		this.kaoshifen = kaoshifen;
	}

	/**
	 * @return the currentpage
	 */
	public int getCurrentpage() {
		return currentpage;
	}

	/**
	 * @param currentpage the currentpage to set
	 */
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	/**
	 * @return the pageresult
	 */
	public int getPageresult() {
		return pageresult;
	}

	/**
	 * @param pageresult the pageresult to set
	 */
	public void setPageresult(int pageresult) {
		this.pageresult = pageresult;
	}

	/**
	 * @return the actiontype
	 */
	public String getActiontype() {
		return actiontype;
	}

	/**
	 * @param actiontype the actiontype to set
	 */
	public void setActiontype(String actiontype) {
		this.actiontype = actiontype;
	}

	/**
	 * @return the actionname
	 */
	public String getActionname() {
		return actionname;
	}

	/**
	 * @param actionname the actionname to set
	 */
	public void setActionname(String actionname) {
		this.actionname = actionname;
	}

	/**
	 * @return the stusex
	 */
	public String getStusex() {
		return stusex;
	}

	/**
	 * @param stusex the stusex to set
	 */
	public void setStusex(String stusex) {
		this.stusex = stusex;
	}

	/**
	 * @return the stuspecialId
	 */
	public String getStuspecialId() {
		return stuspecialId;
	}

	/**
	 * @param stuspecialId the stuspecialId to set
	 */
	public void setStuspecialId(String stuspecialId) {
		this.stuspecialId = stuspecialId;
	}

	/**
	 * @return the specialdepartmentId
	 */
	public String getSpecialdepartmentId() {
		return specialdepartmentId;
	}

	/**
	 * @param specialdepartmentId the specialdepartmentId to set
	 */
	public void setSpecialdepartmentId(String specialdepartmentId) {
		this.specialdepartmentId = specialdepartmentId;
	}

	/**
	 * @return the pagecount
	 */
	public int getPagecount() {
		return pagecount;
	}

	/**
	 * @param pagecount the pagecount to set
	 */
	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	/**
	 * @return the resultcount
	 */
	public int getResultcount() {
		return resultcount;
	}

	/**
	 * @param resultcount the resultcount to set
	 */
	public void setResultcount(int resultcount) {
		this.resultcount = resultcount;
	}

	/**
	 * @return the objects
	 */
	public List<Object> getObjects() {
		return objects;
	}

	/**
	 * @param objects the objects to set
	 */
	public void setObjects(List<Object> objects) {
		this.objects = objects;
	}

	/**
	 * @return the scores
	 */
	public List<Score> getScores() {
		return scores;
	}

	/**
	 * @param scores the scores to set
	 */
	public void setScores(List<Score> scores) {
		this.scores = scores;
	}

	/**
	 * @return the studentcourses
	 */
	public List<Studentcourse> getStudentcourses() {
		return studentcourses;
	}

	/**
	 * @param studentcourses the studentcourses to set
	 */
	public void setStudentcourses(List<Studentcourse> studentcourses) {
		this.studentcourses = studentcourses;
	}

	/**
	 * @return the students
	 */
	public List<Student> getStudents() {
		return students;
	}

	/**
	 * @param students the students to set
	 */
	public void setStudents(List<Student> students) {
		this.students = students;
	}

	/**
	 * @return the courses
	 */
	public List<Course> getCourses() {
		return courses;
	}

	/**
	 * @param courses the courses to set
	 */
	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}

	/**
	 * @return the teachers
	 */
	public List<Teacher> getTeachers() {
		return teachers;
	}

	/**
	 * @param teachers the teachers to set
	 */
	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}

	/**
	 * @return the specials
	 */
	public List<Special> getSpecials() {
		return specials;
	}

	/**
	 * @param specials the specials to set
	 */
	public void setSpecials(List<Special> specials) {
		this.specials = specials;
	}

	/**
	 * @return the departments
	 */
	public List<Department> getDepartments() {
		return departments;
	}

	/**
	 * @param departments the departments to set
	 */
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	/**
	 * @return the coursetypes
	 */
	public List<Coursetype> getCoursetypes() {
		return coursetypes;
	}

	/**
	 * @param coursetypes the coursetypes to set
	 */
	public void setCoursetypes(List<Coursetype> coursetypes) {
		this.coursetypes = coursetypes;
	}

	/**
	 * @return the totalcoursetypes
	 */
	public List<Coursetype> getTotalcoursetypes() {
		return totalcoursetypes;
	}

	/**
	 * @param totalcoursetypes the totalcoursetypes to set
	 */
	public void setTotalcoursetypes(List<Coursetype> totalcoursetypes) {
		this.totalcoursetypes = totalcoursetypes;
	}

	/**
	 * @return the totalspecials
	 */
	public List<Special> getTotalspecials() {
		return totalspecials;
	}

	/**
	 * @param totalspecials the totalspecials to set
	 */
	public void setTotalspecials(List<Special> totalspecials) {
		this.totalspecials = totalspecials;
	}

	/**
	 * @return the totaldepartments
	 */
	public List<Department> getTotaldepartments() {
		return totaldepartments;
	}

	/**
	 * @param totaldepartments the totaldepartments to set
	 */
	public void setTotaldepartments(List<Department> totaldepartments) {
		this.totaldepartments = totaldepartments;
	}

	/**
	 * @return the studentcourseId
	 */
	public String getStudentcourseId() {
		return studentcourseId;
	}

	/**
	 * @param studentcourseId the studentcourseId to set
	 */
	public void setStudentcourseId(String studentcourseId) {
		this.studentcourseId = studentcourseId;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	
	
	
	
}
